package chenjiuxu.seniorui.study;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Shader;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import chenjiuxu.seniorui.R;

/**
 * Created by 15705 on 2017/12/25.
 * BitmapShader  位图渲染
 */
public class BitmapShaderView extends View {
    private Bitmap bitmap;
    private Paint paint;

    public BitmapShaderView(Context context, Paint paint) {
        super(context);
    }

    public BitmapShaderView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint();
        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.img);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.WHITE);
        BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        Matrix matrix = new Matrix();//矩阵缩放 旋转
        float s = (float) getWidth() / bitmap.getWidth();
        matrix.setScale(s, s);
        shader.setLocalMatrix(matrix);
        paint.setShader(shader);

        int r = getWidth() / 2;
        canvas.drawCircle(r, r, r, paint);//画园

        matrix.setScale(s, s);
        canvas.drawBitmap(bitmap, 0, (float) r * 2, paint);//画图
    }


}
